package com.dingli.servlet.ajax;

import com.dingli.DAO.AdminDao;
import com.dingli.entity.Person;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "ListAllUserServlet", value = "/listAllUser")
public class ListAllUserServlet extends HttpServlet {
    private AdminDao adminDao = new AdminDao();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            List<Person> persons = adminDao.showAllPerson();
            request.setAttribute("persons", persons);
            response.getWriter().write(objectMapper.writeValueAsString(persons));
        } catch (SQLException e) {
            response.getWriter().write(objectMapper.writeValueAsString("error"));
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}


